sources <- c("ESPN", "FantasyData", "FantasyPros", "FantasySharks", 
             "FantasyFootballNerd", "NumberFire", "Yahoo",
             "RTSports", "Walterfootball")

scrape <- scrape_data(src = sources,
                      pos = c('QB', 'RB', 'WR', 'TE', 'DST'),
                      season = 2020, 
                      week = week)

Simulation Time!

n_sims <- 10000

tic()
sim_lu <- map_df(1:n_sims, generate_lineup) %>%
  rename(pts_base = points) %>%  
  mutate(position = factor(position, 
                           levels = c("QB", "RB", "WR", "TE", "DST"))) %>% 
  select(lineup, Name, team, position, pts_base, pts_pred, sd_pts, Salary)
toc()
## 298.57 sec elapsed

Results

ggplotly(sim_lu %>% 
           group_by(Name, position, Salary) %>% 
           dplyr::summarize(lu = n_distinct(lineup)) %>% 
           ungroup() %>% 
           group_by(position) %>% 
           top_n(10, lu) %>% 
           ungroup() %>% 
           arrange(position, desc(lu)) %>% 
           mutate(Name = factor(Name),
                  Name = fct_reorder(Name, lu),
                  pct = round(lu / n_sims, 3) * 100) %>% 
           ggplot(aes(x = Name, y = pct, fill = Salary,
                      text = paste(Name, "in", lu, "lineups with", Salary, "salary"))) +
           geom_bar(stat = "identity") +
           facet_wrap(~position, ncol = 2, scales = "free_y") +
           coord_flip() +
           scale_fill_viridis_c() +
           xlab("") +
           ylab("Lineups (thousands)") +
           ggtitle("Top 10 Players By Position")) %>% 
  ggplotly(tooltip = "text")
plyr_lu <- sim_lu %>%
  group_by(Name, position) %>%
  dplyr::summarize(lu=n_distinct(lineup)) %>%
  ungroup() 

ggplotly(projections %>% 
  filter(avg_type=='weighted') %>%
  mutate(Name = ifelse(pos=="DST", last_name, paste(first_name, last_name))) %>%
  inner_join(fan_duel, by = c("Name", "position")) %>%
  select(Name, team, position, points, Salary, sd_pts) %>%
  left_join(plyr_lu, by='Name') %>%
  replace_na(list(lu=0)) %>%
  mutate(lu_bin=ifelse(lu==0, '0 Lineups', '>=1 Lineups'),
         lu_5=cut(lu,5, labels = FALSE)) %>%
  ggplot(aes(x=Salary, y=points, color=lu_bin, size=sd_pts, text=Name)) +
  geom_point() +
  theme_minimal() +
  scale_color_manual(values = c('red', 'blue'), name="") +
  geom_smooth(inherit.aes = FALSE, aes(x=Salary, y=points), method = 'lm') +
  ylab('Projected Points') +
  xlab('Salary') +
  ggtitle('Who makes it into Optimized Lineups?') +
  scale_x_continuous(labels=scales::dollar))
lu_df <- sim_lu %>%
  group_by(lineup) %>%
  dplyr::summarize(lineup_pts=sum(pts_pred),
                   lineup_sd=sum(sd_pts)) %>%
  ungroup()

pto <- psel(lu_df, low(lineup_sd) * high(lineup_pts))


ggplotly(ggplot(lu_df, aes(y=lineup_pts, x=lineup_sd, text = paste0("Lineup: ", lineup))) +
  geom_point() +
  geom_point(data=pto, size=5) +
  ylab('Lineup Points') +
  xlab('Lineup Points St Dev') +
  ggtitle('Lineup Points vs. Uncertainty',
          subtitle = 'Pareto Lineups Bolded'))
psel(lu_df, low(lineup_sd) * high(lineup_pts)) %>%
  left_join(sim_lu, by='lineup') %>%
  group_by(lineup) %>%
  arrange(lineup_pts, position, desc(Salary)) %>%
  select(lineup, lineup_pts, lineup_sd, Name, team, position, pts_pred, sd_pts, Salary) %>%
  mutate_at(vars(lineup_pts, lineup_sd, pts_pred, sd_pts), function(x) round(x, 2)) %>%
  knitr::kable() %>%
  kable_styling(fixed_thead = T) %>%
  column_spec(1:3, bold=TRUE) %>%
  collapse_rows(columns = 1:3, valign = 'top') %>%
  scroll_box(height = '700px', width = '100%')
lineup lineup_pts lineup_sd Name team position pts_pred sd_pts Salary
3467 125.53 4.20 Kyler Murray ARI QB 24.80 0.66 8800
Aaron Jones GBP RB 18.51 0.22 8800
Giovani Bernard CIN RB 13.12 0.87 6200
Duke Johnson HOU RB 12.41 0.25 5800
Stefon Diggs BUF WR 15.32 0.49 7900
Robert Woods LAR WR 13.74 0.36 7200
Brandin Cooks HOU WR 12.08 0.28 6300
Austin Hooper CLE TE 8.11 0.79 5100
New York Giants NYG DST 7.45 0.28 3900
9957 128.52 4.65 Kyler Murray ARI QB 25.19 0.66 8800
Aaron Jones GBP RB 18.99 0.22 8800
James Robinson JAC RB 18.15 1.45 7300
Duke Johnson HOU RB 12.48 0.25 5800
Keenan Allen LAC WR 15.16 0.39 7800
Robert Woods LAR WR 14.12 0.36 7200
Curtis Samuel CAR WR 11.05 0.28 5700
Evan Engram NYG TE 9.04 0.28 5600
Carolina Panthers CAR DST 4.33 0.77 3000
2434 129.59 4.83 Kyler Murray ARI QB 24.76 0.66 8800
Aaron Jones GBP RB 18.69 0.22 8800
James Robinson JAC RB 18.69 1.45 7300
Duke Johnson HOU RB 12.24 0.25 5800
Stefon Diggs BUF WR 15.68 0.49 7900
Brandin Cooks HOU WR 11.88 0.28 6300
DeVante Parker MIA WR 12.23 0.42 6000
Austin Hooper CLE TE 8.25 0.79 5100
New York Giants NYG DST 7.18 0.28 3900
3954 130.90 4.90 Kyler Murray ARI QB 25.93 0.66 8800
Aaron Jones GBP RB 18.50 0.22 8800
Miles Sanders PHI RB 15.98 0.88 7700
Duke Johnson HOU RB 12.52 0.25 5800
Tyler Lockett SEA WR 14.07 0.34 7400
Robby Anderson CAR WR 16.26 1.06 6400
DeVante Parker MIA WR 11.63 0.42 6000
Austin Hooper CLE TE 8.74 0.79 5100
New York Giants NYG DST 7.26 0.28 3900
217 131.01 5.36 Kyler Murray ARI QB 25.70 0.66 8800
Aaron Jones GBP RB 18.45 0.22 8800
James Robinson JAC RB 17.43 1.45 7300
Duke Johnson HOU RB 12.05 0.25 5800
Robert Woods LAR WR 14.20 0.36 7200
Robby Anderson CAR WR 13.11 1.06 6400
DeVante Parker MIA WR 12.23 0.42 6000
Austin Hooper CLE TE 10.03 0.79 5100
Philadelphia Eagles PHI DST 7.81 0.16 4600
6118 132.60 5.61 Kyler Murray ARI QB 24.99 0.66 8800
Aaron Jones GBP RB 18.81 0.22 8800
James Robinson JAC RB 18.10 1.45 7300
Duke Johnson HOU RB 12.36 0.25 5800
Stefon Diggs BUF WR 15.78 0.49 7900
Robby Anderson CAR WR 13.07 1.06 6400
DeVante Parker MIA WR 12.73 0.42 6000
Austin Hooper CLE TE 9.54 0.79 5100
New York Giants NYG DST 7.21 0.28 3900
2751 134.13 5.84 Kyler Murray ARI QB 25.55 0.66 8800
Aaron Jones GBP RB 18.94 0.22 8800
James Robinson JAC RB 16.53 1.45 7300
Duke Johnson HOU RB 12.20 0.25 5800
Robert Woods LAR WR 14.48 0.36 7200
Terry McLaurin WAS WR 18.86 1.43 7000
DeVante Parker MIA WR 12.05 0.42 6000
Austin Hooper CLE TE 8.68 0.79 5100
New York Giants NYG DST 6.85 0.28 3900
1410 134.64 6.65 Josh Allen BUF QB 25.48 1.16 8700
Aaron Jones GBP RB 18.45 0.22 8800
James Robinson JAC RB 19.16 1.45 7300
Jerick McKinnon SFO RB 14.92 1.05 5500
Robert Woods LAR WR 14.18 0.36 7200
Robby Anderson CAR WR 12.81 1.06 6400
DeVante Parker MIA WR 12.42 0.42 6000
Noah Fant DEN TE 9.60 0.39 5700
Tampa Bay Buccaneers TBB DST 7.61 0.55 4200
4133 137.42 6.70 Kyler Murray ARI QB 25.45 0.66 8800
Aaron Jones GBP RB 18.67 0.22 8800
James Robinson JAC RB 20.96 1.45 7300
D’Andre Swift DET RB 14.37 1.10 6000
Terry McLaurin WAS WR 17.20 1.43 7000
Tee Higgins CIN WR 12.21 0.67 6100
DeVante Parker MIA WR 12.15 0.42 6000
Eric Ebron PIT TE 8.59 0.60 5400
Philadelphia Eagles PHI DST 7.82 0.16 4600
4879 137.54 7.64 Josh Allen BUF QB 26.52 1.16 8700
Aaron Jones GBP RB 18.52 0.22 8800
James Robinson JAC RB 18.90 1.45 7300
D’Andre Swift DET RB 13.92 1.10 6000
Keenan Allen LAC WR 15.27 0.39 7800
DeVante Parker MIA WR 12.16 0.42 6000
Jerry Jeudy DEN WR 15.70 1.61 5800
Noah Fant DEN TE 9.20 0.39 5700
Detroit Lions DET DST 7.36 0.90 3900
1999 137.89 7.75 Kyler Murray ARI QB 24.83 0.66 8800
Aaron Jones GBP RB 19.00 0.22 8800
James Robinson JAC RB 18.25 1.45 7300
D’Andre Swift DET RB 15.25 1.10 6000
Robert Woods LAR WR 13.74 0.36 7200
Robby Anderson CAR WR 13.41 1.06 6400
Jerry Jeudy DEN WR 14.89 1.61 5800
Noah Fant DEN TE 9.42 0.39 5700
Detroit Lions DET DST 9.09 0.90 3900
7144 138.41 7.92 Kyler Murray ARI QB 26.42 0.66 8800
Aaron Jones GBP RB 18.53 0.22 8800
James Robinson JAC RB 19.22 1.45 7300
Jerick McKinnon SFO RB 13.18 1.05 5500
Terry McLaurin WAS WR 15.74 1.43 7000
Robby Anderson CAR WR 13.28 1.06 6400
Jerry Jeudy DEN WR 15.13 1.61 5800
Evan Engram NYG TE 9.24 0.28 5600
Philadelphia Eagles PHI DST 7.67 0.16 4600
5704 138.77 8.61 Kyler Murray ARI QB 24.81 0.66 8800
Aaron Jones GBP RB 18.53 0.22 8800
James Robinson JAC RB 17.98 1.45 7300
D’Andre Swift DET RB 14.19 1.10 6000
Keenan Allen LAC WR 15.20 0.39 7800
Jerry Jeudy DEN WR 16.11 1.61 5800
Josh Reynolds LAR WR 9.85 1.26 5000
Darren Waller LVR TE 13.87 0.85 7000
Houston Texans HOU DST 8.22 1.07 3500
6222 140.17 8.78 Justin Herbert LAC QB 26.74 2.37 8000
Aaron Jones GBP RB 18.66 0.22 8800
James Robinson JAC RB 16.95 1.45 7300
D’Andre Swift DET RB 16.15 1.10 6000
Stefon Diggs BUF WR 16.04 0.49 7900
Tyler Boyd CIN WR 13.90 0.98 6700
Jerry Jeudy DEN WR 14.92 1.61 5800
Evan Engram NYG TE 9.44 0.28 5600
New York Giants NYG DST 7.36 0.28 3900
3326 140.19 10.30 Kyler Murray ARI QB 25.40 0.66 8800
Alvin Kamara NOS RB 19.36 0.93 9000
Aaron Jones GBP RB 19.11 0.22 8800
James Robinson JAC RB 18.49 1.45 7300
DeVante Parker MIA WR 11.50 0.42 6000
Jerry Jeudy DEN WR 12.97 1.61 5800
Alshon Jeffery PHI WR 16.40 3.83 4800
Evan Engram NYG TE 9.17 0.28 5600
Detroit Lions DET DST 7.78 0.90 3900
3461 140.23 10.35 Kyler Murray ARI QB 24.70 0.66 8800
Alvin Kamara NOS RB 19.53 0.93 9000
Aaron Jones GBP RB 18.35 0.22 8800
D’Andre Swift DET RB 14.32 1.10 6000
Terry McLaurin WAS WR 16.91 1.43 7000
Jarvis Landry CLE WR 12.54 1.51 6000
Alshon Jeffery PHI WR 17.44 3.83 4800
Noah Fant DEN TE 9.18 0.39 5700
New York Giants NYG DST 7.25 0.28 3900
2736 140.60 10.37 Kyler Murray ARI QB 25.27 0.66 8800
Alvin Kamara NOS RB 17.78 0.93 9000
Aaron Jones GBP RB 18.72 0.22 8800
Jerick McKinnon SFO RB 11.93 1.05 5500
Terry McLaurin WAS WR 15.95 1.43 7000
Robby Anderson CAR WR 13.68 1.06 6400
Alshon Jeffery PHI WR 20.23 3.83 4800
Evan Engram NYG TE 9.34 0.28 5600
Detroit Lions DET DST 7.70 0.90 3900
1073 141.25 10.44 Josh Allen BUF QB 25.25 1.16 8700
Aaron Jones GBP RB 18.71 0.22 8800
James Robinson JAC RB 16.70 1.45 7300
Duke Johnson HOU RB 12.19 0.25 5800
Terry McLaurin WAS WR 15.09 1.43 7000
Tyler Boyd CIN WR 13.96 0.98 6700
Alshon Jeffery PHI WR 20.73 3.83 4800
Darren Waller LVR TE 11.49 0.85 7000
New York Giants NYG DST 7.13 0.28 3900
6435 144.58 10.86 Josh Allen BUF QB 23.89 1.16 8700
Aaron Jones GBP RB 19.12 0.22 8800
James Robinson JAC RB 18.02 1.45 7300
D’Andre Swift DET RB 14.60 1.10 6000
Stefon Diggs BUF WR 16.38 0.49 7900
Terry McLaurin WAS WR 15.69 1.43 7000
Alshon Jeffery PHI WR 19.83 3.83 4800
Evan Engram NYG TE 9.01 0.28 5600
Detroit Lions DET DST 8.04 0.90 3900
9839 145.83 11.33 Justin Herbert LAC QB 28.90 2.37 8000
Aaron Jones GBP RB 18.56 0.22 8800
James Robinson JAC RB 18.33 1.45 7300
Stefon Diggs BUF WR 15.17 0.49 7900
Terry McLaurin WAS WR 17.01 1.43 7000
Tyler Boyd CIN WR 13.68 0.98 6700
Alshon Jeffery PHI WR 17.84 3.83 4800
Evan Engram NYG TE 9.13 0.28 5600
New York Giants NYG DST 7.22 0.28 3900
3718 148.47 14.12 Kyler Murray ARI QB 24.91 0.66 8800
James Robinson JAC RB 18.24 1.45 7300
Zack Moss BUF RB 17.50 2.45 5800
DeeJay Dallas SEA RB 20.15 3.65 5600
Stefon Diggs BUF WR 15.58 0.49 7900
Terry McLaurin WAS WR 15.31 1.43 7000
Tyler Boyd CIN WR 14.65 0.98 6700
Darren Waller LVR TE 11.75 0.85 7000
Las Vegas Raiders LVR DST 10.38 2.18 3500